package com.cn.qtms.common.result;//类注释设置模板


import java.time.Instant;

/**
 * 进行web结果的包装方法
 * 主要提供成功时的方法success和错误时的error方法
 * @author find
 *
 */
public class ResultUtil {
	
	private ResultUtil() {
		
	}

	public static <T> Result<T> success(){
		Result<T> result = new Result<>();
		result.setCode(ErrorCode.SUCCESS.getCode());
		result.setMsg(ErrorCode.SUCCESS.getMessage());
		result.setTimestamp(Instant.now());
		return result;
	}

	public static <T> Result<T> success(T data){
		Result<T> result = new Result<>();
		result.setCode(ErrorCode.SUCCESS.getCode());
		result.setMsg(ErrorCode.SUCCESS.getMessage());
		result.setData(data);
		result.setTimestamp(Instant.now());
		return result;
	}
	
	public static <T> Result<T> error(IResultCode result) {
		return error(result.getCode(), result.getMessage());
	}
	
	public static <T> Result<T> error(Integer code, String msg){
		Result<T> res = new Result<>();
		res.setCode(code);
		res.setMsg(msg);
		res.setTimestamp(Instant.now());
		return res;
	}

	public static <T> Result<T> error(){
		return error(ErrorCode.UNKNOW_FAILED);
	}
	public static <T> Result<T> error(ErrorCode code){
		return error(code.getCode(), code.getMessage());
	}

}
